package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmy.cultivate.entity.CancellationRate;
import com.xmy.cultivate.entity.SchoolQuarter;
import com.xmy.cultivate.entity.StaffStatusCount;
import com.xmy.cultivate.service.ISchoolQuarterService;
import com.xmy.cultivate.service.IStaffService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 员工 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2025-02-25
 */
@Controller
@RestController
@RequestMapping("/api/staff")
public class StaffController {

    @Autowired
    IStaffService iStaffService;

    @Autowired
    ISchoolQuarterService iSchoolQuarterService;

    @GetMapping("/getStaffStatusCountList")
    @ApiOperation("在职入职-获取各校区列表")
    public Result getStaffList(@RequestParam(value = "type",required = false) String type,  //校区
                               @RequestParam(value = "yearClassId",required = false) String yearClassIds,
                               @RequestParam(value = "subjectsId",required = false) String subjectsIds) {
        List<CancellationRate> staffStatusCountList = null;
        // 判断 yearClassIds 是否为空或为 null
        List<String> yearClassId = yearClassIds != null && !yearClassIds.isEmpty()
                ? Arrays.asList(yearClassIds.split(","))
                : Arrays.asList();

        // 判断 subjectsIds 是否为空或为 null
        List<String> subjectsId = subjectsIds != null && !subjectsIds.isEmpty()
                ? Arrays.asList(subjectsIds.split(","))
                : Arrays.asList();
        //校区
        if (type.equals("4")){
            staffStatusCountList = iStaffService.getStaffStatusCountList(yearClassId, subjectsId);
        }
        //年级
        if (type.equals("7")){
            staffStatusCountList = iStaffService.getStaffStatusCountByGradeList(subjectsId);
        }
        //四大校区
        if (type.equals("6")){

            staffStatusCountList = iStaffService.getStaffStatusCountByFourSchoolList(yearClassId, subjectsId);

            List<CancellationRate> collectList = new ArrayList<>();

            QueryWrapper queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("deleted", 0);
            queryWrapper.eq("pid", 0);
            queryWrapper.groupBy("school_id");
            List<SchoolQuarter> schoolQuarterList = iSchoolQuarterService.list(queryWrapper);

            for (SchoolQuarter schoolQuarter : schoolQuarterList) {
                CancellationRate arrivalRate = new CancellationRate();
                arrivalRate.setSchoolId(schoolQuarter.getSchoolId());
                arrivalRate.setSchoolName(schoolQuarter.getSchoolName());
                arrivalRate.setSchoolPid(schoolQuarter.getPid());
                arrivalRate.setWorkingCount(0);
                arrivalRate.setLeaveCount(0);
                collectList.add(arrivalRate);
            }

            for (CancellationRate arrivalRate : collectList) {
                List<CancellationRate> childRates = new ArrayList<>();
                for (CancellationRate arrivalRateChild : staffStatusCountList) {
                    if (arrivalRate.getSchoolId().equals(arrivalRateChild.getSchoolId()) || arrivalRate.getSchoolId().equals(arrivalRateChild.getSchoolPid())) {
                        childRates.add(arrivalRateChild);
                        arrivalRate.setWorkingCount(arrivalRateChild.getWorkingCount() + arrivalRate.getWorkingCount());
                        arrivalRate.setLeaveCount(arrivalRateChild.getLeaveCount() + arrivalRate.getLeaveCount());
                    }
                }
                arrivalRate.setChildRateList(childRates);
            }

            staffStatusCountList= collectList;

        }
        return new Result(Code.OK, staffStatusCountList, Code.GET_OK_MSG);
    }

    @GetMapping("/getStaffStatusDetails")
    @ApiOperation("在职入职-获取校区详情列表")
    public Result getStaffStatusDetails(@RequestParam(value = "type",required = false) String type,  //校区
                                        @RequestParam(value = "yearClassId",required = false) String yearClassIds,
                                        @RequestParam(value = "subjectsId",required = false) String subjectsIds,
                                        @RequestParam(value = "schoolId",required = false) String schoolId,
                                        @RequestParam(value = "status",required = false) String status  //1：在职，2:离职
    ){

        // 判断 yearClassIds 是否为空或为 null
        List<String> yearClassId = yearClassIds != null && !yearClassIds.isEmpty()
                ? Arrays.asList(yearClassIds.split(","))
                : Arrays.asList();

        // 判断 subjectsIds 是否为空或为 null
        List<String> subjectsId = subjectsIds != null && !subjectsIds.isEmpty()
                ? Arrays.asList(subjectsIds.split(","))
                : Arrays.asList();
        List<StaffStatusCount> staffStatusDetails = null;

        if (type.equals("7")){
            staffStatusDetails = iStaffService.getStaffStatusGradeDetails(yearClassIds, subjectsId, status);
        }else {
            staffStatusDetails = iStaffService.getStaffStatusDetails(yearClassId, subjectsId, schoolId, status);
        }


//        List<StaffStatusCount> staffStatusDetails = iStaffService.getStaffStatusDetails(yearClassId, subjectsId, schoolId, status);
        return new Result(Code.OK, staffStatusDetails, Code.GET_OK_MSG);
    }

}

